import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.vo.product.crf.CreditCRFResultDto;
import com.vcredit.creditcat.exception.ProductCanNotApplyException;
import com.vcredit.creditcat.service.product.xrf.XRFApplyResultServiceImpl;
import com.vcredit.creditcat.util.Encrypt;
import com.vcredit.creditcat.util.HttpRequestExt;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.JacksonUtil;

import io.netty.util.internal.StringUtil;

public class CrfService {
	@Value("${crf.return.url}")
	private String returnUrl;

	@Value("${resource.image.url}")
	private String imagUrl;

	@Value("${crf.app.channel}")
	private String appChannel;

	@Value("${crf.salesman.no}")
	private String salesmanNo;

	@Value("${crf.agent.no}")
	private String agentNo;

	/** 对接用的url **/
	private String creditCrftestUrl="https://app-uat.crfchina.com/creditApply/apiApply";

	private String dataSource="test_0002";

	private String key="test_0002_crf0803";

	@Value("${crf.promotion.url}")
	private String promotionUrl;

	@Value("${crf.promotion.code}")
	private String promotionCode;

	@Value("${crf.promotion.key}")
	private String promotionKey;

	private static Logger logger = LoggerFactory.getLogger(XRFApplyResultServiceImpl.class);

	/**
	 * 提交资料
	 * 
	 * @param submitDto
	 * @return
	 * @throws Exception
	 */
	@Log
	public Map<String, String> commit(Object submitDto) throws Exception {
		Map<String, String> resultMap = new HashMap<>();
		String json = JacksonUtil.Obj2Json(submitDto);
		String signature = Encrypt.base64HmacMD5(key, json);
		signature = signature.replaceAll("\r\n", "");
		logger.info("signature :" + signature);
		try {
			Header[] headers = new Header[3];
			headers[0] = new BasicHeader("X-CrfChina-Signature", signature);
			headers[1] = new BasicHeader("X-Data-Source", dataSource);
			headers[2] = new BasicHeader("Content-Type", "application/json");
			String resultJson = HttpRequestExt.doPut(creditCrftestUrl + "/createOrder", json, "UTF-8", headers);
			if (StringUtil.isNullOrEmpty(resultJson)) {
				resultMap.put("status", "00");
				return resultMap;
			} else {
				String decodeJsonParam = resultJson.replace(":\"\"", ":null");
				CreditCRFResultDto result = JacksonUtil.Json2Obj(decodeJsonParam, new TypeReference<CreditCRFResultDto>() {
				});
				logger.info(result.getMessage());
				resultMap.put("status", "01");
				resultMap.put("message", result.getMessage());
				return resultMap;
			}
		} catch (Exception e) {
			logger.error("------------信而富申请出错!!", e);
			throw e;
		}
	}
	@Test
	public void checkCredit() throws ProductCanNotApplyException, Exception {
		Map<String, String> resultMap = new HashMap<>();
		resultMap.put("orderId", "XYM_11");

		String json = JacksonUtil.Obj2Json(resultMap);
		String signature = Encrypt.base64HmacMD5(key, json);
		logger.info("signature :" + signature);
		signature = signature.replaceAll("\r\n", "");
		logger.info("signature :" + signature);
		try {
			Header[] headers = new Header[3];
			headers[0] = new BasicHeader("X-CrfChina-Signature", signature);
			headers[1] = new BasicHeader("X-Data-Source", dataSource);
			headers[2] = new BasicHeader("Content-Type", "application/json");
			String resultJson = HttpRequestExt.doPost(creditCrftestUrl.concat("/orderStatus") , json, "UTF-8", headers);
			if (StringUtil.isNullOrEmpty(resultJson)) {
				resultMap.put("status", "00");
			} else {
				String decodeJsonParam = resultJson.replace(":\"\"", ":null");
				CreditCRFResultDto result = JacksonUtil.Json2Obj(decodeJsonParam, new TypeReference<CreditCRFResultDto>() {
				});
				logger.info(result.getMessage());
				resultMap.put("status", "01");
				resultMap.put("message", result.getMessage());
			}
		} catch (Exception e) {
			logger.error("------------信而富申请出错!!", e);
			throw e;
		}
	}

}
